package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class MessageController {

    // 获取环信的账号信息
    @GetMapping("/huanxin/user")
    public ResponseEntity getHuanxinUser() {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.封装huanxinVo
        HuanXinVo huanXinVo = new HuanXinVo();
        huanXinVo.setUsername("HX" + userId);
        huanXinVo.setPassword("123456");
        // 3.返回vo
        return ResponseEntity.ok(huanXinVo);
    }

    @Autowired
    private MessageManager messageManager;

    // 好友申请
    @PostMapping("/tanhua/strangerQuestions")
    public void strangerQuestions(@RequestBody Map<String, String> param) {
        // 1.接收参数
        long jiarenId = Long.parseLong(param.get("userId"));
        String reply = param.get("reply");

        // 2.调用manager
        messageManager.strangerQuestions(jiarenId, reply);
    }

    // 添加好友
    @PostMapping("/messages/contacts")
    public void addContacts(@RequestBody Map<String, Long> param) {
        // 1.接收参数
        Long friendId = param.get("userId");
        // 2.调用manager
        messageManager.addContacts(friendId);
    }

    // 查看好友列表
    @GetMapping("/messages/contacts")
    public ResponseEntity findFriendByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 调用manager
        return messageManager.findFriendByPage(pageNum, pageSize);
    }

    @Autowired
    private UserManager userManager;

    // 根据环信id查询用户信息
    @GetMapping("/messages/userinfo")
    public ResponseEntity findByUserInfoVo(String huanxinId) {  // HX99
        Long userId = Long.parseLong(huanxinId.replace("HX", ""));
        return userManager.findUserInfoVo(userId);
    }

    // 查询我的点赞信息
    @GetMapping("/messages/likes")
    public ResponseEntity findMovementLikes(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 指定类型
        Integer commentType = 1;
        // 调用manager
        return messageManager.findMovmenetComment(commentType, pageNum, pageSize);
    }
    // 查看我的评论信息
    @GetMapping("/messages/comments")
    public ResponseEntity findMovementComments(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 指定类型
        Integer commentType = 2;
        // 调用manager
        return messageManager.findMovmenetComment(commentType, pageNum, pageSize);
    }

    // 查看我的喜欢信息
    @GetMapping("/messages/loves")
    public ResponseEntity findMovementLoves(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 指定类型
        Integer commentType = 3;
        // 调用manager
        return messageManager.findMovmenetComment(commentType, pageNum, pageSize);
    }
}
